﻿using System;
using System.Windows;
using mmUI.Managers;

namespace mmUI.Windows
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        private bool _mCanceled = false;
        private bool _mSaved = false;

        public bool Canceled
        {
            get { return _mCanceled; }
        }

        public LoginWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if(string.IsNullOrEmpty(_mTbUser.Text) || string.IsNullOrEmpty(_mTbPass.Password))
            {
                MessageBox.Show("One or more fields are empty.", "Cannot continue", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            Config.Password = _mTbPass.Password;
            Config.GoogleLogin = _mTbUser.Text;
            Config.SavePassword = _mRemember.IsChecked.HasValue ? _mRemember.IsChecked.Value : false;
            _mSaved = true;
            Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if(!_mSaved)
            {
                _mCanceled = true;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(Config.GoogleLogin))
            {
                _mTbUser.Text = Config.GoogleLogin;
                _mTbPass.Focus();
            }
            else
            {
                _mTbUser.Focus();    
            }
        }
    }
}
